/**
 * MyProductSrv.java
 * 
 * Project: GWBS2009
 * 
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2007-2010 Shanghai Auto-Parts E-Procurement Co., Ltd.
 *        All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        APEP Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with APEP.
 *
 * Warning:
 * =============================================================================
 *
 */
package system.base;

import system.LoginContext;

import com.hitrust.gs.lib.GSBaseConstants;

import framework.core.DBConnection;
import framework.core.IHandler;
import framework.core.ILoginContext;
import framework.core.LogWriter;
import framework.core.Logger;
import framework.core.SearchEvent;
import framework.core.SrvContext;

public class MyProductSrv extends IHandler {

	public void doAction(DBConnection con, SrvContext context, ILoginContext loginContext) throws Exception {
		String actionCode = context.getActionCode();
		String path = "/system/base/";

		if(actionCode.equalsIgnoreCase("listMyProduct")) {
			listMyProduct(con, context, loginContext, path);
			return ;
		}

		if(actionCode.equalsIgnoreCase("addToMyProduct")) {
			String[] ids = context.getParameterValues("ids");

			for(int i=0; i<ids.length; i++) {
				MyProduct.insertMyProduct(con, loginContext, loginContext.getLoginWarehouseCode(), ids[i]);
			}
			context.setErrorMsg("我的产品已成功添加。");
			if(GSBaseConstants.PLANPRICE.equals("true")){
				context.setValue("isPlanprice", "1");
			}else{
				context.setValue("isPlanprice", "0");
			}
			context.setNextUrl(path+"ProductList.jsp");
			return;
		}

		Logger.throwException("MenuInfoSrv","unknown action_code="+actionCode);
	}

	private void listMyProduct(DBConnection con,SrvContext context, ILoginContext loginContext, String path) throws Exception {
		LogWriter.debug("我的产品列表");

		MyProductSearchEvent event = (MyProductSearchEvent) SearchEvent.fromForm(context, MyProductSearchEvent.class);

		LoginContext tLoginContext = (LoginContext)loginContext;
		event.setCurrentWarehouseCode(tLoginContext.getLoginWarehouseCode());

		if(GSBaseConstants.PLANPRICE.equals("true")){
			context.setValue("isPlanprice", "1");
		}else{
			context.setValue("isPlanprice", "0");
		}
		context.setSessionValue("MyProductSearchEvent", event);
		context.setNextUrl(path + "MyProductList.jsp");

	}

}
